* comparing local linear RDs with different bandwidths, donuts and controls 


*local controls_lla0 "" // IFO LS i.dow // unrate i.dow i.q  // i.m i.q // built dwt loa draft beam power_kw USflag
local controls_lla1 IFO LS // unrate i.dow i.q  // i.m i.q // built dwt loa draft beam power_kw USflag
*local controls_lla2 IFO LS unrate // i.m i.q // built dwt loa draft beam power_kw USflag
*local controls_lla3 IFO LS c.unrate##c.unrate // i.m i.q // built dwt loa draft beam power_kw USflag

*local controls_ves built dwt loa draft beam power_kw USflag group_agg_i
*local fe_ves "i.route_id"	



local rte_opts all_exits all_coast 
local fe_vesrte "i.route_id#i.ves_id"	
local eca_ys 2009 // 2011 // 2009 // 2011 
local folder rd_regs
local vce "cluster ves_id"

* different bandwidths and donuts (basic specification)
local vestype "Container"	
local donuts 0 // 0 15 30 // 45
local bws  90 120 150 180 365 // 240

	
foreach eca_y of local eca_ys {
		
	if `eca_y' == 2009 {
		local run t_eca09
		local cut eca1
		local placebo 365
		
		local ports `" inlist(port_agg,"SoCal","NoCal") "'
		local y_tag "09"
	}
	if `eca_y'==2011 {
		local run t_eca11 
		local cut eca2
		local ports `" inlist(port_agg,"SoCal") "' // only socal
		local y_tag "11"
	}
	
	* set trends
	*local xvar "i.`cut'#c.`run'" 
	local xvar_port "i.`cut'#c.`run'#i.route_id"
		
		
	* Y variables
	*** Y variables
	if "`y_tag'"=="09" {
		local yvars dist_eca`eca_y' avg_speed_eca`eca_y' f_eca`y_tag'_cons cost_eca`y_tag'_cons e_pm_eca`y_tag'_cons td_inm_eca`y_tag'_cons
	}
	else {
		local yvars dist_eca`eca_y' avg_speed_eca`eca_y' f_eca`y_tag'_cons cost_eca`y_tag'_cons e_pm_eca`y_tag'_cons td_inm_eca`y_tag'_cons  
	}
	*local yvars dist_eca20`y_tag' dist f_eca`y_tag'_cons f_cons cost_comply_cons td_inm_comply_cons // dist_eca20`y_tag' f_eca`y_tag'_cons   
		
	foreach yvar of local yvars {
		foreach donut of local donuts {	
			
			foreach rte_opt of local rte_opts {

				local apprep replace
				local out_file "`folder'/ll_bw_levels_062021_`rte_opt'_`yvar'_eca`eca_y'.tex"	
			
				* default route fixed effects
				local rte_fe route_id

				if "`rte_opt'"=="all_coast" {
					local rte_cond 	`" `ports' & route_type=="Coastal" "' 
					local samp_lab "port-port"
				}
						
				if "`rte_opt'"=="all_exits" {
					local rte_cond 	`" `ports' & route_type=="EnterExit"  "' 
					local rte_fe route_agg // collapsing exit routes
					local samp_lab "ent/exit"
				}		
			
				capture drop tmp_samp
				gen tmp_samp = (sample_RD_regs==1) & `rte_cond' & vesseltype_regstr=="`vestype'" 
				
				
				capture drop cut
				gen cut = `cut'
				local lbl : variable label `cut'
				label var cut `"`lbl'"' 
				
				foreach bw of local bws {
				
					reghdfe `yvar' cut `xvar_port' `controls_lla1' /// 
						if tmp_samp==1 & abs(`run')<=`bw'  & ( `run'<-`donut' | `run'>`donut' ) ///
						, absorb(`fe_vesrte') vce(`vce')
						
					outreg2 using `out_file', `apprep' lab keep( cut ) tex(frag) nonotes nocon /// 
								addstat( Vessels , e(N_clust))  addtext( Bandwidth - days, `bw' ) ctitle("") // Sample, `samp_lab' ,
					local apprep append				
													
				}

									
				* run placebos
				foreach bw of local bws {
					capture drop run_p
					capture drop cut
					gen run_p = `run' - `placebo'
					gen cut = (run_p>=0) 
					label var cut `"`lbl'"' 
					
					local xvar_port_p "i.cut#c.run_p#i.route_id"
										
					reghdfe `yvar' cut `xvar_port_p' `controls_lla1' /// 
						if tmp_samp==1 & abs(run_p)<=`bw' & ( run_p<-`donut' | run_p>`donut' ) ///
						, absorb(`fe_vesrte') vce(`vce')
											
					outreg2 using `out_file', `apprep' lab keep( cut ) tex(frag) nonotes nocon /// 
								addstat( Vessels , e(N_clust))  addtext(  Bandwidth - days, `bw' , Cut Shift - days , `placebo' ) ctitle("")
					local apprep append				
													
				}
				
				/*	
				* trying to add optimal bw
				*** lots of difficulties here
				* how to include fixed effects??
				* how to include multiple trends
				*clear matrix
				capture drop fe_g 
				capture drop fe_dums*
				qui egen fe_g = group( `rte_fe' ) if tmp_samp==1 //  ves_id
				qui tab fe_g if tmp_samp==1 , gen(fe_dums)  
				*drop fe_dums1
				
				capture drop res
				local robust_bw 250
				reghdfe `yvar'  if tmp_samp==1 & ( abs(`run')<=`robust_bw' ) , absorb(`fe_vesrte') resid(res)

				** rdplot res `run' if reg_samp , p(2) binselect(esmv) // covs(IFO LS fe_dums*) covs_eval(0) 

				rdrobust `yvar' `run' /// 
					if tmp_samp==1 & ( abs(`run')<=150 ) & ( `run'<-`donut' | `run'>`donut' ) /// 
				, c(0) p(1) h(150) masspoints(off) vce(`vce') covs( LS IFO fe_dums* ) kernel(uniform) all	 //  bwcheck(200)  
				
				outreg2 using `out_file',  `apprep' lab  tex(frag) nonotes nocon /// 
				/// addstat( Vessels , e(N_clust))  ///
				addstat(Bandwidth , e(b_l) ) /// 
				addtext( Sample, `samp_lab' ) ctitle("")

				asdsad
				
				rdrobust res `run' /// 
					if tmp_samp==1 & ( abs(`run')<=`robust_bw' ) & ( `run'<-`donut' | `run'>`donut' ) /// 
				, c(0) p(1) bwselect(msetwo) masspoints(off) vce(`vce') covs( LS IFO ) kernel(uniform) all	 // covs( fe_dums* ) //  bwcheck(200)  kernel(uniform) 	
								
				outreg2 using `out_file',  `apprep' lab  tex(frag) nonotes nocon /// 
						/// addstat( Vessels , e(N_clust))  ///
						addstat(Bandwidth , e(b_l) ) /// 
						addtext( Sample, `samp_lab' ) ctitle("")
				
				asdasd
				*/
				
				drop tmp_samp
			}

		}
	}	
}

				
				
/*
		foreach exposed of local exposeds {
				
				
				
			* label
			local exposed_tag Y
			if `exposed'==0 {
				local exposed_tag N
			}
			
			local yvar_use `yvar'	
				
			if `exposed'==0 & ( "`yvar'"=="ldist_eca20`y_tag'" | "`yvar'"=="f_eca`y_tag'_cons" ) {
				capture drop ltmp
				capture drop tmp
				gen ltmp = 0 
				gen tmp = 0 

				* add labels
				local lbl : variable label `yvar'
				label var tmp `"`lbl'"' 

				local yvar_use tmp
				
			}
	*/
				
